<!--#
layout("/layouts/platform.html"){
#-->
<style>
    .el-upload input[type=file] {
        display: none;
    }

    .el-upload__text {
        color: #606266;
        font-size: 14px;
        text-align: center;
    }
</style>
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <a class="el-button el-button--default el-button--medium" href="${base}/platform/sys/app" data-pjax><i
                    class="ti-angle-left"></i>返回</a>
            <el-button size="medium" @click="openAdd">
                <i class="ti-plus"></i>
                添加安装包
            </el-button>
        </div>
        <div class="btn-group tool-button mt5">
            <el-input @keyup.native="doSearch" placeholder="请输入内容" v-trim v-model="pageForm.appName" size="medium">
                <template slot="prepend">实例名称</template>
            </el-input>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                style="width: 100%"
                :default-sort = "{prop: 'opAt', order: 'descending'}"
        >
            <el-table-column
                    sortable
                    prop="appName"
                    label="实例名称"
                    header-align="center"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    sortable
                    prop="appVersion"
                    label="版本号"
                    width="100"
            >
            </el-table-column>
            <el-table-column
                    prop="fileSize"
                    label="文件大小"
                    width="120"
            >
                <template slot-scope="scope">
                    {{(scope.row.fileSize/1024/1024).toFixed(2)}}MB
                </template>
            </el-table-column>
            <el-table-column
                    prop="filePath"
                    label="文件下载"
                    header-align="center">
                <template slot-scope="scope">
                    <a style="color: #0e5996" :href="'${base!}/platform/sys/app/jar/download/'+scope.row.id" target="_blank">{{scope.row.appName}}-{{scope.row.appVersion}}.jar</a>
                </template>
            </el-table-column>
            <el-table-column
                    sortable
                    prop="opAt"
                    label="上传时间"
                    header-align="center" align="center">
                <template slot-scope="scope">
                    {{formatAt(scope.row.opAt)}}
                </template>
            </el-table-column>
            <el-table-column
                    sortable
                    prop="opAt"
                    label="上传人"
                    header-align="center" align="center">
                <template slot-scope="scope">
                    {{scope.row.user.loginname}}
                </template>
            </el-table-column>
            <el-table-column
                    sortable
                    prop="disabled"
                    label="启用状态"
                    header-align="center"
                    align="center"
                    width="100"
            >
                <template slot-scope="scope">
                    <i v-if="scope.row.disabled" class="fa fa-circle text-danger ml5"></i>
                    <i v-if="!scope.row.disabled" class="fa fa-circle text-success ml5"></i>
                </template>
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="120">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                                    :command="{type:'enable',row:scope.row}">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item
                                    :command="{type:'disable',row:scope.row}">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item divided
                                              :command="{type:'delete',row:scope.row}">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
    <el-dialog :close-on-click-modal="false"
            title="添加安装包"
            :visible.sync="addDialogVisible"
            width="50%">
        <el-form :model="formData" ref="addForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item prop="appName" label="实例名称">
                <el-autocomplete
                        class="inline-input"
                        v-trim v-model="formData.appName"
                        :fetch-suggestions="queryAppName"
                        placeholder="保持和配置文件中实例名称一致"
                        style="width: 220px;"
                ></el-autocomplete>
            </el-form-item>
            <el-form-item prop="appVersion" label="版本号">
                <el-input maxlength="20" placeholder="版本号不可用特殊字符"
                          v-trim v-model="formData.appVersion"
                          auto-complete="off" tabindex="2" type="text"
                          style="width: 220px;"></el-input>
            </el-form-item>
            <el-form-item prop="filePath" label="文件上传">
                <el-upload
                        class="upload-demo"
                        drag
                        action="${base}/open/file/upload/file"
                        name="Filedata"
                        :show-file-list="false"
                        :before-upload="beforeJarUpload"
                        :on-success="function(resp,file,fileList){return handleJarSuccess(resp,file,fileList)}"
                        :on-progress="uploadFileProcess"
                        style="height:180px;"
                        v-if="!formData.fileSize||formData.fileSize==0"
                >
                    <i class="el-icon-upload" v-if="!fileUploadFlag"></i>
                    <div class="el-upload__text" v-if="!fileUploadFlag">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传 jar 文件，且不超过200MB</div>
                    <el-progress v-if="fileUploadFlag == true" type="circle" :percentage="fileUploadPercent" style="margin-top:20px;"></el-progress>
                </el-upload>
                <div v-if="formData.fileSize>0" style="color: green;font-size: 12px;">
                    文件大小: {{(formData.fileSize/1024/1024).toFixed(2)}}MB
                </div>

            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doAdd">确 定</el-button>
        </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                addDialogVisible: false,
                editDialogVisible: false,
                visiableData: [],
                tableData: [],
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    pageOrderName: "opAt",
                    pageOrderBy: "descending",
                    appName: ""
                },
                formData: {
                    id: "",
                    fileSize: 0,
                    filePath: ""
                },
                formRules: {
                    appName: [
                        {required: true, message: '实例名称', trigger: ['blur', 'change']}
                    ],
                    appVersion: [
                        {required: true, message: '版本号', trigger: ['blur', 'change']}
                    ],
                    filePath: [
                        {required: true, message: '上传文件', trigger: ['blur', 'change']}
                    ],
                },
                fileUploadFlag:false,
                fileUploadPercent:0
            }
        },
        methods: {
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                $.post(base + "/platform/sys/app/jar/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            formatAt: function (val) {
                if (val && val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            openAdd: function () {
                this.addDialogVisible = true;
                this.formData = {
                    id: "",
                    fileSize: 0,
                    filePath: ""
                };//打开新增窗口,表单先清空
                if (this.$refs["addForm"])
                    this.$refs["addForm"].resetFields();
            },
            doAdd: function () {
                var self = this;
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {
                        $.post(base + "/platform/sys/app/jar/addDo", self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                            self.pageData();//添加失败也可以入库,所以加载一下看看
                        }, "json");
                    }
                });
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                if ("enable" == command.type || "disable" == command.type) {
                    $.post(base + "/platform/sys/app/jar/" + command.type + "/" + command.row.id, {}, function (data) {
                        if (data.code == 0) {
                            self.$message({
                                message: data.msg,
                                type: 'success'
                            });
                            if ("disable" == command.type) {
                                command.row.disabled = true;
                            }
                            if ("enable" == command.type) {
                                command.row.disabled = false;
                            }
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                if ("delete" == command.type) {
                    self.$confirm('此操作将删除 ' + command.row.appName+'-'+command.row.appVersion+'.jar', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" == a) {//确认后再执行
                                $.post(base + "/platform/sys/app/jar/delete/" + command.row.id, {}, function (data) {
                                    if (data.code == 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            },
            beforeJarUpload: function (file) {
                var isLt120M = file.size / 1024 / 1024 < 200;
                if (".jar" != file.name.substring(file.name.lastIndexOf("."), file.name.length)) {
                    this.$message.error('只可上传jar文件');
                    return false;
                }
                if (!isLt120M) {
                    this.$message.error('文件大小不能超过200MB');
                    return false;
                }
                return true;
            },
            handleJarSuccess: function (response, file, fileList) {
                if (response.code == 0) {
                    // 对象属性直接赋值不会触发试图更新，采用 Vue.set 、this.$set或者Object.assign({}，this.obj)创建新对象
                    // 或者也可以定义一个 refresh 属性 ，当数据发生改变时，该属性同时变化，也会触发视图更新
                    this.$set(this.formData, "fileSize", response.data.file_size);
                    this.$set(this.formData, "filePath", response.data.file_url);
                } else {
                    this.$message({
                        message: response.msg,
                        type: 'error'
                    });
                    this.$set(this.formData, "fileSize", 0);
                    this.$set(this.formData, "filePath", "");
                }
                this.fileUploadFlag=false;
            },
            uploadFileProcess:function(event, file, fileList){
                this.fileUploadFlag=true;
                this.fileUploadPercent = parseInt(event.percent);
            },
            queryAppName: function (appName, cb) {
                $.post(base + "/platform/sys/app/jar/search", {appName: appName}, function (data) {
                    if (data.code == 0) {
                        var list=data.data;
                        var names=[];
                        list.forEach(function (obj) {
                            names.push({value:obj});
                        });
                        cb(names);
                    }
                }, "json");
            }
        },
        created: function () {
            this.doSearch();
        }
    });
</script>

<!--#
}
#-->
